package com.job.meeting.controller;

import com.google.gson.Gson;
import com.job.meeting.pojo.*;
import com.job.meeting.service.bookingService;
import com.job.meeting.service.meetingRoomService;
import com.job.meeting.service.userService;
import com.job.meeting.tools.wechatSystem;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.chrono.MinguoDate;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/management")
public class managementController {
    @Autowired
    userService us;
    @Autowired
    bookingService bs;
    @Autowired
    meetingRoomService mrs;

    wechatSystem system = null;

    private enum Type {
        NOT_PASS, APPROVING, PASS
    }

    //todo: management页面跳转
    @RequestMapping("/page")//TODO:暂未测试
    public String page()
    {
        return "management";
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private class showingMessage {
        int userID;
        boolean state;
        String userNickname;
        String userRealName;
        String meetingRoomName;
        String bookingPurpose;
        int bookingParticipants;
        bookingTime bookingTimeOccupied;
        int bookingState;
        String bookingComment;
        String bookingReply;
        int bookingID;
        int meetingRoomID;

        showingMessage(meetingRoom room, user u, booking b, boolean state)
        {
            this.setBookingComment(b.getComment());
            this.setBookingParticipants(b.getParticipants());
            this.setBookingPurpose(b.getPurpose());
            this.setBookingReply(b.getReply());
            this.setBookingState(b.getStatus());
            this.setBookingTimeOccupied(new Gson().fromJson(b.getTime_occupied(), bookingTime.class));
            this.setMeetingRoomName(room.getName());
            this.setState(state);
            this.setUserID(u.getId());
            this.setUserNickname(u.getNickname());
            this.setUserRealName(u.getReal_name());
            this.bookingID = b.getId();
            this.meetingRoomID = room.getID();
        }
    }


    //初始化，根据openid找用户
    @ResponseBody
    @GetMapping("/greeting")
    public user init(@RequestParam("openid") String openid) {
        system = new wechatSystem(us);
        return us.getUserByOpenid(openid);
    }

    //获得全部的尚未确认的预约。如果权限不足，
    //那么返回的json字段将表明这种错误。
    @ResponseBody
    @GetMapping("/getAllUncheckBookingSubmit")
    public List<showingMessage> getAllUncheckBookingSubmit(@RequestParam("openid") String openID) {
        //todo: 正式上线后删除
        system = new wechatSystem(us);

        showingMessage message = new showingMessage();
        message.setState(us.PrivilegesVerification(openID));
        List<showingMessage> showingMessages = new ArrayList<>();
        if (message.isState()) {
            List<booking> bookings = bs.getAllBookingByState(Type.APPROVING.ordinal());
            for (booking b : bookings) {
                meetingRoom room = mrs.getMeetingRoom(b.getMeeting_room_id());
                user u = us.getUserByUserId(b.getUser_id());
                showingMessages.add(new showingMessage(room, u, b, true));
            }
        } else {
            showingMessages.add(message);
        }
        return showingMessages;
    }

    //传入用户的openid，booking的id和状态state，更新booking状态，返回是否成功
    @ResponseBody
    @RequestMapping("/updateBookingState")
    public boolean updateBookingState(@RequestBody String json, @RequestParam("openid") String openID) {
        //todo: 正式上线后删除
        system = new wechatSystem(us);

        booking b = new booking(json);
        meetingRoom room = mrs.getMeetingRoom(b.getMeeting_room_id());
        wechatMessageMeetingUpdate update = new wechatMessageMeetingUpdate();
        update.setDate(new unit(new Gson().fromJson(b.getTime_occupied(), bookingTime.class).getTime()));
        update.setMeetingRoom(new unit(room.getName()));
        update.setReply(new unit(b.getReply()));
        update.setState(b.getStatus());
        system.sendMessage(update, openID);

        return bs.updateBooking(b) > 0;
    }

    @ResponseBody
    @GetMapping("/getAllHistoryByOpenID")
    public List<showingMessage> getAllHistoryByOpenID(@RequestParam("openid") String openID)
    {
        user u = us.getUserByOpenid(openID);
        List<booking> bookings = bs.getAllBookingByUserID(u.getId());
        List<showingMessage> messages = new ArrayList<>();
        for(booking b: bookings)
        {
            meetingRoom room = mrs.getMeetingRoom(b.getMeeting_room_id());
            showingMessage message = new showingMessage(room, u, b, true);
            messages.add(message);
        }
        return messages;
    }

    @ResponseBody
    @GetMapping("/getBookingByID")
    public booking getBookingByID(@RequestParam("id") String id)
    {
        return bs.getBookingByID(Integer.parseInt(id));
    }
    /*
    //根据传入的openID判断权限，根据传入的json字段来增加会议室，返回增加结果
    public String addMeetingRoom(String json, String openID) {
        return "";
    }

    //根据传入的openID和json字段来删除某个会议室。结果将以json字段的形式返回。
    public String deleteMeetingRoom(String openID, String jsonIncludeID) {
        return "";
    }

    //根据openID判断权限，根据传入的json字段判断要改变的状态。
    public String ChangeMeetingRoomState(String oenID, String json) {
        return "";
    }

    //根据传入的openID判断权限，根据传入的json字段生成对应的二维码。
    // 返回二维码的临时URL以供查看。
    public String GenQRCode(String openID, String json) {
        return "";
    }
     */

    //跳转去booking
    @GetMapping("/toBooking")
    public String toBooking(@RequestParam("openid") String openid) {
        return "redirect:/booking/page?openid=" + openid;
    }

    //跳转去management
    @GetMapping("/toManagement")
    public String toManagement(@RequestParam("openid") String openid) {
        return "redirect:/management/page?openid=" + openid;
    }

    //跳转去meetingRoom
    @GetMapping("/toMeetingRoom")
    public String toMeetingRoom(@RequestParam("openid") String openid) {
        return "redirect:/meetingRoom/page?openid=" + openid;
    }

}
